<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Testing dojo._data / NodeList.data</title>
		
		<script type="text/javascript" src="../dojo.js" djConfig="isDebug: true"></script>
		<script type="text/javascript">
			dojo.require("doh.runner");
			dojo.require("dojo.NodeList-data");
			dojo.addOnLoad(function(){
				
				var $ = dojo.query;
				var len = function(obj){
					var x = 0;
					for(var i in obj){ x++ }
					return x;
				};
				
				doh.register("t", 
					[
						
						function sanity(t){
							
							var list = $("#foo");
							var lis = $("#bar > li");
							
							t.is(1, list.length, "i'm not insane");
							t.is(2, lis.length, "li's are sane, too");
							
							list.data("a", "b");
							lis.data("a", "b");
							
							t.is("b", list.data("a")[0]);
							t.is(["b"], list.data("a"));
							
							t.is(["b","b"], lis.data("a"));
							t.is("b", lis.data("a")[0]);
						},
					
						function basicdata(t){
						
							var list = $('#foo');
							
							list.data("bar", 6)
								.data("baz", "a")
								.data("bam", [1,2,3])
								.data("foo", { a:"b" })
							;
							
							var newlist = $("#foo");
							
							t.is(6, newlist.data("bar")[0]);
							t.is("a", newlist.data("baz")[0]);
							t.is(3, newlist.data("bam")[0].length);
							t.is(1, newlist.data("bam")[0][0]);
							t.is("b", newlist.data("foo")[0].a);
							
						},
						
						function hashdata(t){
							
							$("#foo").data({
								bar:"baz",
								foo:"bap"
							});
							
							t.is("baz", $('#foo').data("bar")[0]);
							t.is("bap", $('#foo').data("foo")[0]);
							
						},
						
						function butdoesitchain(t){
							
							$("#foo").data("bar", 42).style("color", "red");
							t.is(42, $("#foo").data("bar")[0]);
						},
						
						function getanobjectback(t){
							
							$("#foo").data("afoo", 1);
							$("#foo").data("bfoo", 2);
							
							var obj = $("#foo").data()[0];
							
							t.is(1, obj.afoo);
							t.is(2, obj.bfoo);
							
						},
						
						function plaindata(t){
							dojo.query("#bar li").data("bar", 42)
								.forEach(function(n){
									t.is(42, dojo._nodeData(n, "bar"));
								});
						},
						
						function removeData(t){
							dojo.query("#bar li").removeData("bar");
							dojo.query("#bar li").forEach(function(n){
								t.t(!dojo._nodeData(n, "bar"));
							});
							
							dojo.query("#bar li").data({
								a:"b", c:"d", e:"f"
							});
							
							dojo.query("#bar li").removeData();
							var data = dojo.query("#bar li").data()[0];
							
							t.f(data.a);
							t.f(data.c);
							t.f(data.e);
						},
						
						function multidata(t){
							
							var ret = dojo.query("#bar li");
							t.is(2, ret.length, "sanity: 2 (0..1) li's in query");
							ret = ret.data("bar", "baz").data();
							
							
							t.is(ret[0].bar, "baz", "item 0 was set");
							t.is(ret[1].bar, "baz", "item 1 was set");
							
							dojo.query("li").at(0).removeData();
							
							var ret2 = dojo.query("#bar li").data();
							t.is(ret.length, 2, "sanity: 2 (0..1) li's in query");
							t.f(ret2[0].bar, "at(0) was removed");
							t.is(ret2[1].bar, "baz", "at(1) was untouched");
							
						},
						
						function obj(t){
							var x = dojo.query("#foo").data({ bar: { baz:"bam" }}).data("bar");
							t.is("bam", x[0].baz);
							
						},
						
						function cleanData(t){

							if(!dojo._nodeDataCache){
								t.t("We must be testing a built Dojo. No access to dataCache");
								return;
							}
							
							var me = dojo.query("#bar li").data("die", "yes");
							me.at(0).attr("id", "killme");
							var data = me.data();
							
							t.is(2, me.length);
							t.is("yes", me.data("die")[0]);
							t.is("yes", me.data("die")[1]);
							
							var l = len(dojo._nodeDataCache);
							
							dojo.destroy("killme");
							dojo._gcNodeData();
							
							t.is(l - 1, len(dojo._nodeDataCache), "one item removed because destroyed");
							me = dojo.query("#bar li");
							t.is(1, me.length);
							
						},
						
						function literals(t){
							// this is an implementation detail. object literals count,
							// but doesn't mean you should use them.
							var x = { a:1 };
							
							var one = dojo.query("#lit span").data("literal", x);
							x.a++;
							
							t.is(2, dojo.query("#lit span").data("literal")[0].a);
							t.is(2, dojo.query("#lit span").data("literal")[1].a);
							
						},
						
						function clearall(t){

							if(!dojo._nodeDataCache){
								t.t("We must be testing a built Dojo. No access to dataCache");
								return;
							}
							
							var l = len(dojo._nodeDataCache);
							t.t(l, "there is stuff in the cache");;
							
							dojo.query("#b > *").forEach(dojo.destroy);
							dojo._gcNodeData();
							t.is(0, len(dojo._nodeDataCache), "no longer stuff in the cache");
						}
					]
				);
				doh.run();
			});
		</script>
	</head>
	<body><div id="b">
		<div id="foo">woot.</div>
		<ul id="bar">
			<li>baz</li>
			<li>bam</li>
		</ul>
		<p id="lit"><span>hi</span><span>there</span></p>
		</div>
	</body>
</html>
